<!DOCTYPE html>
<html>
<head>
    <title>django-websocket</title>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">//<![CDATA[
    $(function () {
        $('#send_message').click(function () {
            var socket = new WebSocket("ws://" + window.location.host + "/echo_once/");
            socket.onopen = function () {
                console.log('WebSocket open');//成功连接上Websocket
                socket.send($('#message').val());//发送数据到服务端
            };
            socket.onmessage = function (e) {
                console.log('message: ' + e.data);//打印服务端返回的数据
                $('#messagecontainer').prepend('<p>' + e.data + '</p>');
            };
        });
    });
    //]]></script>
</head>
<body>
<br>
<input type="text" id="message" value="Hello, World!"/>
<button type="button" id="send_message">发送 message</button>
<h1>Received Messages</h1>
<div id="messagecontainer">

</div>
</body>
</html>